package com.onlyxiahui.common.dispatcher.config;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.onlyxiahui.common.dispatcher.impl.ServiceActionInterceptor;
import com.onlyxiahui.framework.action.dispatcher.ActionDispatcher;
import com.onlyxiahui.framework.action.dispatcher.config.ActionConfigurer;
import com.onlyxiahui.framework.action.dispatcher.extend.ActionBox;
import com.onlyxiahui.framework.action.dispatcher.registry.ActionBoxRegistry;
import com.onlyxiahui.framework.action.dispatcher.registry.ActionInterceptorRegistry;
import com.onlyxiahui.framework.json.validator.ValidatorService;

/**
 * Date 2019-04-20 08:13:26<br>
 * Description
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */
public class ServiceActionDispatcher extends ActionDispatcher {

	ServiceActionConfigurer handlerActionConfigurer = new ServiceActionConfigurer();
	ServiceActionInterceptor handlerActionInterceptor = new ServiceActionInterceptor(this);
//	 ServiceAuthInterceptor handlerAuthInterceptor = new ServiceAuthInterceptor();

	public ServiceActionDispatcher() {
		this.init();
	}

	public ServiceActionDispatcher(ActionBox actionBox) {
		this.init();
		this.add(actionBox);
	}

	public ServiceActionDispatcher(ActionBox actionBox, String... actionLocations) {
		this.init();
		this.add(actionBox);
		this.scan(actionLocations);
	}

	public void addActionValidator(String actionValidatorConfigPaht) {
		// "classpath*:/validator/main/action/*.json"
		ValidatorService validatorService = new ValidatorService(actionValidatorConfigPaht);
		handlerActionInterceptor.setValidatorService(validatorService);
	}

	public void setActionValidateProperty(String validateProperty) {
		handlerActionInterceptor.setValidateProperty(validateProperty);
	}

	@Override
	public void addConfig(ActionConfigurer actionConfigurer) {
		super.addConfig(actionConfigurer);
	}

	private void init() {
		this.addConfig(handlerActionConfigurer);
		this.addConfig(new ServiceActionConfigurerAdapter() {
			@Override
			public void addActionInterceptor(ActionInterceptorRegistry registry) {
				registry.add(handlerActionInterceptor);
				// registry.add(handlerAuthInterceptor);
			}
		});
	}

	private void add(ActionBox actionBox) {
		// "com.oim.server.general.main.business.*.action"
		this.addConfig(new ServiceActionConfigurerAdapter() {
			@Override
			public void addActionBox(ActionBoxRegistry registry) {
				registry.add(actionBox);
			}
		});
	}

	@Override
	public Object doInvoke(Object bean, Method method, Object... args) {
		try {
			makeAccessible(method);
			return method.invoke(bean, args);
		} catch (IllegalArgumentException e) {
			throw e;
		} catch (RuntimeException e) {
			throw e;
		} catch (InvocationTargetException e) {
			Throwable t = e.getTargetException();
			if (t instanceof RuntimeException) {
				throw (RuntimeException) t;
			}
			String message = (e.getMessage() != null ? e.getMessage() : "方法调用异常");
			throw new IllegalStateException(message, e);
		} catch (IllegalAccessException e) {
			Throwable t = e.getCause();
			if (t instanceof RuntimeException) {
				throw (RuntimeException) t;
			}
			String message = (e.getMessage() != null ? e.getMessage() : "非法访问");
			throw new IllegalStateException(message, e);
		}
	}
}
